package com.it.sun.redismq.swagger;

/**
 * @ClassName com.it.sun.redismq.swagger.Swagger2Configuration
 * @Author sunmannian
 * @Date 2021/1/28 10:29
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 * Copyright (C) XXX公司  since 2021/1/28
 */

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author sun
 * @Date 2021/1/28 10:21
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 * Copyright (C) xxxx  since 2021/1/28
 */
@Configuration
@EnableSwagger2()
@Profile({"dev", "test"})
public class Swagger2Configuration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler ("swagger-ui.html")
                .addResourceLocations ("classpath:/META-INF/resources/");

        registry.addResourceHandler ("/webjars/**")
                .addResourceLocations ("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler ("doc.html").addResourceLocations ("classpath:/META-INF/resources/");
    }

    /**
     * swagger-ui.html路径映射，浏览器中使用/api-docs访问
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController ("/swagger", "/swagger-ui.html").setStatusCode (HttpStatus.PERMANENT_REDIRECT);
        registry.addRedirectViewController ("/doc", "/doc.html").setStatusCode (HttpStatus.PERMANENT_REDIRECT);
    }

    @Bean
    public Docket createRestApi() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder ();
        List<Parameter> pars = new ArrayList<Parameter> ();
        tokenPar.name ("Authorization").description ("令牌").modelRef (new ModelRef ("string")).parameterType ("header").required (false).build ();
        pars.add (tokenPar.build ());
        return new Docket (DocumentationType.SWAGGER_2)
//                .host(host)
                .apiInfo (apiInfo ())
                .select ()
                .apis (RequestHandlerSelectors.basePackage ("com.it.sun"))
                .paths (PathSelectors.any ())
                .build ()
                .securityContexts (Lists.newArrayList (securityContext ()))
                .securitySchemes (Lists.<SecurityScheme>newArrayList (apiKey ()))
                /* .globalOperationParameters(pars)*/;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder ()
                .title ("RESTful APIs")
                .description ("平台模块API")
                .termsOfServiceUrl ("#")
                .contact (new Contact ("redis简易消息队列", "#", "zdxf@zdxf.com"))
                .version ("1.0")
                .license ("license")
                .licenseUrl ("")
                .build ();
    }

    private ApiKey apiKey() {
        return new ApiKey ("BearerToken", "Authorization", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder ()
                .securityReferences (defaultAuth ())
                .forPaths (PathSelectors.regex ("/.*"))
                .build ();
    }


    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope ("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList (new SecurityReference ("BearerToken", authorizationScopes));
    }

}




